

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="zh" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="zh" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>optuna.integration.sklearn &mdash; Optuna 1.4.0 文档</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
        <script src="../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> Optuna
          

          
          </a>

          
            
            
              <div class="version">
                1.4.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">目录</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">安装</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../tutorial/index.html">教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference/index.html">API Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../faq.html">常见问题</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Optuna</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">模块代码</a> &raquo;</li>
        
      <li>optuna.integration.sklearn</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>optuna.integration.sklearn 源代码</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">DEBUG</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">INFO</span>
<span class="kn">from</span> <span class="nn">logging</span> <span class="kn">import</span> <span class="n">WARNING</span>
<span class="kn">from</span> <span class="nn">numbers</span> <span class="kn">import</span> <span class="n">Integral</span>
<span class="kn">from</span> <span class="nn">numbers</span> <span class="kn">import</span> <span class="n">Number</span>
<span class="kn">from</span> <span class="nn">time</span> <span class="kn">import</span> <span class="n">time</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">scipy</span> <span class="k">as</span> <span class="nn">sp</span>

<span class="kn">from</span> <span class="nn">optuna._imports</span> <span class="kn">import</span> <span class="n">try_import</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">distributions</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">TrialPruned</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">logging</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">samplers</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">study</span> <span class="k">as</span> <span class="n">study_module</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna.study</span> <span class="kn">import</span> <span class="n">StudyDirection</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">trial</span> <span class="k">as</span> <span class="n">trial_module</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna.trial</span> <span class="kn">import</span> <span class="n">FrozenTrial</span>  <span class="c1"># NOQA</span>
<span class="kn">from</span> <span class="nn">optuna</span> <span class="kn">import</span> <span class="n">type_checking</span>  <span class="c1"># NOQA</span>

<span class="k">if</span> <span class="n">type_checking</span><span class="o">.</span><span class="n">TYPE_CHECKING</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">scipy.sparse</span> <span class="kn">import</span> <span class="n">spmatrix</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Any</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Callable</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Iterable</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">List</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Mapping</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Union</span>  <span class="c1"># NOQA</span>

    <span class="n">ArrayLikeType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">,</span> <span class="n">spmatrix</span><span class="p">]</span>
    <span class="n">OneDimArrayLikeType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">]</span>
    <span class="n">TwoDimArrayLikeType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]],</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span> <span class="n">spmatrix</span><span class="p">]</span>
    <span class="n">IterableType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">List</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">,</span> <span class="n">spmatrix</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span>
    <span class="n">IndexableType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="n">Iterable</span><span class="p">,</span> <span class="kc">None</span><span class="p">]</span>

<span class="k">with</span> <span class="n">try_import</span><span class="p">()</span> <span class="k">as</span> <span class="n">_imports</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">sklearn</span>
    <span class="kn">from</span> <span class="nn">sklearn.base</span> <span class="kn">import</span> <span class="n">BaseEstimator</span>
    <span class="kn">from</span> <span class="nn">sklearn.base</span> <span class="kn">import</span> <span class="n">clone</span>
    <span class="kn">from</span> <span class="nn">sklearn.base</span> <span class="kn">import</span> <span class="n">is_classifier</span>
    <span class="kn">from</span> <span class="nn">sklearn.metrics.scorer</span> <span class="kn">import</span> <span class="n">check_scoring</span>
    <span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">BaseCrossValidator</span>  <span class="c1"># NOQA</span>
    <span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">check_cv</span>
    <span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="kn">import</span> <span class="n">cross_validate</span>
    <span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">check_random_state</span>
    <span class="kn">from</span> <span class="nn">sklearn.utils.metaestimators</span> <span class="kn">import</span> <span class="n">_safe_split</span>

    <span class="k">if</span> <span class="n">sklearn</span><span class="o">.</span><span class="n">__version__</span> <span class="o">&gt;=</span> <span class="s2">&quot;0.22&quot;</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">_safe_indexing</span> <span class="k">as</span> <span class="n">sklearn_safe_indexing</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="kn">from</span> <span class="nn">sklearn.utils</span> <span class="kn">import</span> <span class="n">safe_indexing</span> <span class="k">as</span> <span class="n">sklearn_safe_indexing</span>
    <span class="kn">from</span> <span class="nn">sklearn.utils.validation</span> <span class="kn">import</span> <span class="n">check_is_fitted</span>

<span class="k">if</span> <span class="ow">not</span> <span class="n">_imports</span><span class="o">.</span><span class="n">is_successful</span><span class="p">():</span>
    <span class="n">BaseEstimator</span> <span class="o">=</span> <span class="nb">object</span>  <span class="c1"># NOQA</span>


<span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">get_logger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_check_fit_params</span><span class="p">(</span>
    <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: TwoDimArrayLikeType</span>
    <span class="n">fit_params</span><span class="p">,</span>  <span class="c1"># type: Dict</span>
    <span class="n">indices</span><span class="p">,</span>  <span class="c1"># type: OneDimArrayLikeType</span>
<span class="p">):</span>
    <span class="c1"># type: (...) -&gt; Dict</span>

    <span class="n">fit_params_validated</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">fit_params</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>

        <span class="c1"># NOTE Original implementation:</span>
        <span class="c1"># https://github.com/scikit-learn/scikit-learn/blob/ \</span>
        <span class="c1"># 2467e1b84aeb493a22533fa15ff92e0d7c05ed1c/sklearn/utils/validation.py#L1324-L1328</span>
        <span class="c1"># Scikit-learn does not accept non-iterable inputs.</span>
        <span class="c1"># This line is for keeping backward compatibility.</span>
        <span class="c1"># (See: https://github.com/scikit-learn/scikit-learn/issues/15805)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">_is_arraylike</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="ow">or</span> <span class="n">_num_samples</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">!=</span> <span class="n">_num_samples</span><span class="p">(</span><span class="n">X</span><span class="p">):</span>
            <span class="n">fit_params_validated</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">fit_params_validated</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">_make_indexable</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="n">fit_params_validated</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">_safe_indexing</span><span class="p">(</span><span class="n">fit_params_validated</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">indices</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">fit_params_validated</span>


<span class="c1"># NOTE Original implementation:</span>
<span class="c1"># https://github.com/scikit-learn/scikit-learn/blob/ \</span>
<span class="c1"># 8caa93889f85254fc3ca84caa0a24a1640eebdd1/sklearn/utils/validation.py#L131-L135</span>
<span class="k">def</span> <span class="nf">_is_arraylike</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># type: (Any) -&gt; bool</span>

    <span class="k">return</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s2">&quot;__len__&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s2">&quot;shape&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s2">&quot;__array__&quot;</span><span class="p">)</span>


<span class="c1"># NOTE Original implementation:</span>
<span class="c1"># https://github.com/scikit-learn/scikit-learn/blob/ \</span>
<span class="c1"># 8caa93889f85254fc3ca84caa0a24a1640eebdd1/sklearn/utils/validation.py#L217-L234</span>
<span class="k">def</span> <span class="nf">_make_indexable</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
    <span class="c1"># type: (IterableType) -&gt; (IndexableType)</span>

    <span class="n">tocsr_func</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="s2">&quot;tocsr&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">tocsr_func</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">sp</span><span class="o">.</span><span class="n">sparse</span><span class="o">.</span><span class="n">issparse</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">tocsr_func</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="s2">&quot;__getitem__&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="s2">&quot;iloc&quot;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">iterable</span>
    <span class="k">elif</span> <span class="n">iterable</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">iterable</span>
    <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_num_samples</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="c1"># type: (ArrayLikeType) -&gt; int</span>

    <span class="c1"># NOTE For dask dataframes</span>
    <span class="c1"># https://github.com/scikit-learn/scikit-learn/blob/ \</span>
    <span class="c1"># 8caa93889f85254fc3ca84caa0a24a1640eebdd1/sklearn/utils/validation.py#L155-L158</span>
    <span class="n">x_shape</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s2">&quot;shape&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">x_shape</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">Integral</span><span class="p">):</span>
            <span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">x_shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Expected sequence or array-like, got </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">_safe_indexing</span><span class="p">(</span>
    <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: Union[OneDimArrayLikeType, TwoDimArrayLikeType]</span>
    <span class="n">indices</span><span class="p">,</span>  <span class="c1"># type: OneDimArrayLikeType</span>
<span class="p">):</span>
    <span class="c1"># type: (...) -&gt; Union[OneDimArrayLikeType, TwoDimArrayLikeType]</span>
    <span class="k">if</span> <span class="n">X</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">X</span>

    <span class="k">return</span> <span class="n">sklearn_safe_indexing</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">indices</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">_Objective</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Callable that implements objective function.</span>

<span class="sd">    Args:</span>
<span class="sd">        estimator:</span>
<span class="sd">            Object to use to fit the data. This is assumed to implement the</span>
<span class="sd">            scikit-learn estimator interface. Either this needs to provide</span>
<span class="sd">            ``score``, or ``scoring`` must be passed.</span>

<span class="sd">        param_distributions:</span>
<span class="sd">            Dictionary where keys are parameters and values are distributions.</span>
<span class="sd">            Distributions are assumed to implement the optuna distribution</span>
<span class="sd">            interface.</span>

<span class="sd">        X:</span>
<span class="sd">            Training data.</span>

<span class="sd">        y:</span>
<span class="sd">            Target variable.</span>

<span class="sd">        cv:</span>
<span class="sd">            Cross-validation strategy.</span>

<span class="sd">        enable_pruning:</span>
<span class="sd">            If :obj:`True`, pruning is performed in the case where the</span>
<span class="sd">            underlying estimator supports ``partial_fit``.</span>

<span class="sd">        error_score:</span>
<span class="sd">            Value to assign to the score if an error occurs in fitting. If</span>
<span class="sd">            &#39;raise&#39;, the error is raised. If numeric,</span>
<span class="sd">            ``sklearn.exceptions.FitFailedWarning`` is raised. This does not</span>
<span class="sd">            affect the refit step, which will always raise the error.</span>

<span class="sd">        fit_params:</span>
<span class="sd">            Parameters passed to ``fit`` one the estimator.</span>

<span class="sd">        groups:</span>
<span class="sd">            Group labels for the samples used while splitting the dataset into</span>
<span class="sd">            train/validation set.</span>

<span class="sd">        max_iter:</span>
<span class="sd">            Maximum number of epochs. This is only used if the underlying</span>
<span class="sd">            estimator supports ``partial_fit``.</span>

<span class="sd">        return_train_score:</span>
<span class="sd">            If :obj:`True`, training scores will be included. Computing</span>
<span class="sd">            training scores is used to get insights on how different</span>
<span class="sd">            hyperparameter settings impact the overfitting/underfitting</span>
<span class="sd">            trade-off. However computing training scores can be</span>
<span class="sd">            computationally expensive and is not strictly required to select</span>
<span class="sd">            the hyperparameters that yield the best generalization</span>
<span class="sd">            performance.</span>

<span class="sd">        scoring:</span>
<span class="sd">            Scorer function.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">estimator</span><span class="p">,</span>  <span class="c1"># type: BaseEstimator</span>
        <span class="n">param_distributions</span><span class="p">,</span>  <span class="c1"># type: Mapping[str, distributions.BaseDistribution]</span>
        <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: TwoDimArrayLikeType</span>
        <span class="n">y</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
        <span class="n">cv</span><span class="p">,</span>  <span class="c1"># type: BaseCrossValidator</span>
        <span class="n">enable_pruning</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">error_score</span><span class="p">,</span>  <span class="c1"># type: Union[Number, str]</span>
        <span class="n">fit_params</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
        <span class="n">groups</span><span class="p">,</span>  <span class="c1"># type: Optional[OneDimArrayLikeType]</span>
        <span class="n">max_iter</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">return_train_score</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">scoring</span><span class="p">,</span>  <span class="c1"># type: Callable[..., Number]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cv</span> <span class="o">=</span> <span class="n">cv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">enable_pruning</span> <span class="o">=</span> <span class="n">enable_pruning</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span> <span class="o">=</span> <span class="n">error_score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">estimator</span> <span class="o">=</span> <span class="n">estimator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fit_params</span> <span class="o">=</span> <span class="n">fit_params</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">groups</span> <span class="o">=</span> <span class="n">groups</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span> <span class="o">=</span> <span class="n">max_iter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span> <span class="o">=</span> <span class="n">param_distributions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span> <span class="o">=</span> <span class="n">return_train_score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scoring</span> <span class="o">=</span> <span class="n">scoring</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="n">X</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>

    <span class="k">def</span> <span class="fm">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (trial_module.Trial) -&gt; float</span>

        <span class="n">estimator</span> <span class="o">=</span> <span class="n">clone</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_params</span><span class="p">(</span><span class="n">trial</span><span class="p">)</span>

        <span class="n">estimator</span><span class="o">.</span><span class="n">set_params</span><span class="p">(</span><span class="o">**</span><span class="n">params</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">enable_pruning</span><span class="p">:</span>
            <span class="n">scores</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cross_validate_with_pruning</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">estimator</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">scores</span> <span class="o">=</span> <span class="n">cross_validate</span><span class="p">(</span>
                <span class="n">estimator</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span>
                <span class="n">cv</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">cv</span><span class="p">,</span>
                <span class="n">error_score</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">error_score</span><span class="p">,</span>
                <span class="n">fit_params</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">fit_params</span><span class="p">,</span>
                <span class="n">groups</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">groups</span><span class="p">,</span>
                <span class="n">return_train_score</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">,</span>
                <span class="n">scoring</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">scoring</span><span class="p">,</span>
            <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_store_scores</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">trial</span><span class="o">.</span><span class="n">user_attrs</span><span class="p">[</span><span class="s2">&quot;mean_test_score&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_cross_validate_with_pruning</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">trial</span><span class="p">,</span>  <span class="c1"># type: trial_module.Trial</span>
        <span class="n">estimator</span><span class="p">,</span>  <span class="c1"># type: BaseEstimator</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; Dict[str, OneDimArrayLikeType]</span>

        <span class="k">if</span> <span class="n">is_classifier</span><span class="p">(</span><span class="n">estimator</span><span class="p">):</span>
            <span class="n">partial_fit_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fit_params</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
            <span class="n">classes</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unique</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>

            <span class="n">partial_fit_params</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s2">&quot;classes&quot;</span><span class="p">,</span> <span class="n">classes</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">partial_fit_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fit_params</span>

        <span class="n">n_splits</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cv</span><span class="o">.</span><span class="n">get_n_splits</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">groups</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">groups</span><span class="p">)</span>
        <span class="n">estimators</span> <span class="o">=</span> <span class="p">[</span><span class="n">clone</span><span class="p">(</span><span class="n">estimator</span><span class="p">)</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_splits</span><span class="p">)]</span>
        <span class="n">scores</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;fit_time&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">n_splits</span><span class="p">),</span>
            <span class="s2">&quot;score_time&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">n_splits</span><span class="p">),</span>
            <span class="s2">&quot;test_score&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">n_splits</span><span class="p">),</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">:</span>
            <span class="n">scores</span><span class="p">[</span><span class="s2">&quot;train_score&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">n_splits</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">step</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cv</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">groups</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">groups</span><span class="p">)):</span>
                <span class="n">out</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_partial_fit_and_score</span><span class="p">(</span><span class="n">estimators</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">train</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="n">partial_fit_params</span><span class="p">)</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">:</span>
                    <span class="n">scores</span><span class="p">[</span><span class="s2">&quot;train_score&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">out</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

                <span class="n">scores</span><span class="p">[</span><span class="s2">&quot;test_score&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">out</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">scores</span><span class="p">[</span><span class="s2">&quot;fit_time&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">out</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">scores</span><span class="p">[</span><span class="s2">&quot;score_time&quot;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">out</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>

            <span class="n">intermediate_value</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">scores</span><span class="p">[</span><span class="s2">&quot;test_score&quot;</span><span class="p">])</span>

            <span class="n">trial</span><span class="o">.</span><span class="n">report</span><span class="p">(</span><span class="n">intermediate_value</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="n">step</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">trial</span><span class="o">.</span><span class="n">should_prune</span><span class="p">():</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_store_scores</span><span class="p">(</span><span class="n">trial</span><span class="p">,</span> <span class="n">scores</span><span class="p">)</span>

                <span class="k">raise</span> <span class="n">TrialPruned</span><span class="p">(</span><span class="s2">&quot;trial was pruned at iteration </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">step</span><span class="p">))</span>

        <span class="k">return</span> <span class="n">scores</span>

    <span class="k">def</span> <span class="nf">_get_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">):</span>
        <span class="c1"># type: (trial_module.Trial) -&gt; Dict[str, Any]</span>

        <span class="k">return</span> <span class="p">{</span>
            <span class="n">name</span><span class="p">:</span> <span class="n">trial</span><span class="o">.</span><span class="n">_suggest</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">distribution</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
        <span class="p">}</span>

    <span class="k">def</span> <span class="nf">_partial_fit_and_score</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">estimator</span><span class="p">,</span>  <span class="c1"># type: BaseEstimator</span>
        <span class="n">train</span><span class="p">,</span>  <span class="c1"># type: List[int]</span>
        <span class="n">test</span><span class="p">,</span>  <span class="c1"># type: List[int]</span>
        <span class="n">partial_fit_params</span><span class="p">,</span>  <span class="c1"># type: Dict[str, Any]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; List[Number]</span>

        <span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span> <span class="o">=</span> <span class="n">_safe_split</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">train</span><span class="p">)</span>
        <span class="n">X_test</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">_safe_split</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">test</span><span class="p">,</span> <span class="n">train_indices</span><span class="o">=</span><span class="n">train</span><span class="p">)</span>

        <span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">estimator</span><span class="o">.</span><span class="n">partial_fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="o">**</span><span class="n">partial_fit_params</span><span class="p">)</span>

        <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span> <span class="o">==</span> <span class="s2">&quot;raise&quot;</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">e</span>

            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">error_score</span><span class="p">,</span> <span class="n">Number</span><span class="p">):</span>
                <span class="n">fit_time</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span>
                <span class="n">test_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span>
                <span class="n">score_time</span> <span class="o">=</span> <span class="mf">0.0</span>

                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">:</span>
                    <span class="n">train_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;error_score must be &#39;raise&#39; or numeric.&quot;</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">fit_time</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span>
            <span class="n">test_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoring</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">)</span>
            <span class="n">score_time</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">fit_time</span> <span class="o">-</span> <span class="n">start_time</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">:</span>
                <span class="n">train_score</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">scoring</span><span class="p">(</span><span class="n">estimator</span><span class="p">,</span> <span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>

        <span class="c1"># Required for type checking but is never expected to fail.</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">fit_time</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span>
        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">score_time</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span>

        <span class="n">ret</span> <span class="o">=</span> <span class="p">[</span><span class="n">test_score</span><span class="p">,</span> <span class="n">fit_time</span><span class="p">,</span> <span class="n">score_time</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">:</span>
            <span class="n">ret</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">train_score</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">ret</span>

    <span class="k">def</span> <span class="nf">_store_scores</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial</span><span class="p">,</span> <span class="n">scores</span><span class="p">):</span>
        <span class="c1"># type: (trial_module.Trial, Dict[str, OneDimArrayLikeType]) -&gt; None</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">array</span> <span class="ow">in</span> <span class="n">scores</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;test_score&quot;</span><span class="p">,</span> <span class="s2">&quot;train_score&quot;</span><span class="p">]:</span>
                <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">score</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">array</span><span class="p">):</span>
                    <span class="n">trial</span><span class="o">.</span><span class="n">set_user_attr</span><span class="p">(</span><span class="s2">&quot;split</span><span class="si">{}</span><span class="s2">_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">name</span><span class="p">),</span> <span class="n">score</span><span class="p">)</span>

            <span class="n">trial</span><span class="o">.</span><span class="n">set_user_attr</span><span class="p">(</span><span class="s2">&quot;mean_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nanmean</span><span class="p">(</span><span class="n">array</span><span class="p">))</span>
            <span class="n">trial</span><span class="o">.</span><span class="n">set_user_attr</span><span class="p">(</span><span class="s2">&quot;std_</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">nanstd</span><span class="p">(</span><span class="n">array</span><span class="p">))</span>


<div class="viewcode-block" id="OptunaSearchCV"><a class="viewcode-back" href="../../../reference/integration.html#optuna.integration.OptunaSearchCV">[文档]</a><span class="k">class</span> <span class="nc">OptunaSearchCV</span><span class="p">(</span><span class="n">BaseEstimator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Hyperparameter search with cross-validation.</span>

<span class="sd">    .. warning::</span>

<span class="sd">        This feature is experimental. The interface may be changed in the future.</span>

<span class="sd">    Args:</span>
<span class="sd">        estimator:</span>
<span class="sd">            Object to use to fit the data. This is assumed to implement the</span>
<span class="sd">            scikit-learn estimator interface. Either this needs to provide</span>
<span class="sd">            ``score``, or ``scoring`` must be passed.</span>

<span class="sd">        param_distributions:</span>
<span class="sd">            Dictionary where keys are parameters and values are distributions.</span>
<span class="sd">            Distributions are assumed to implement the optuna distribution</span>
<span class="sd">            interface.</span>

<span class="sd">        cv:</span>
<span class="sd">            Cross-validation strategy. Possible inputs for cv are:</span>

<span class="sd">            - integer to specify the number of folds in a CV splitter,</span>
<span class="sd">            - a CV splitter,</span>
<span class="sd">            - an iterable yielding (train, validation) splits as arrays of indices.</span>

<span class="sd">            For integer, if :obj:`estimator` is a classifier and :obj:`y` is</span>
<span class="sd">            either binary or multiclass,</span>
<span class="sd">            ``sklearn.model_selection.StratifiedKFold`` is used. otherwise,</span>
<span class="sd">            ``sklearn.model_selection.KFold`` is used.</span>

<span class="sd">        enable_pruning:</span>
<span class="sd">            If :obj:`True`, pruning is performed in the case where the</span>
<span class="sd">            underlying estimator supports ``partial_fit``.</span>

<span class="sd">        error_score:</span>
<span class="sd">            Value to assign to the score if an error occurs in fitting. If</span>
<span class="sd">            &#39;raise&#39;, the error is raised. If numeric,</span>
<span class="sd">            ``sklearn.exceptions.FitFailedWarning`` is raised. This does not</span>
<span class="sd">            affect the refit step, which will always raise the error.</span>

<span class="sd">        max_iter:</span>
<span class="sd">            Maximum number of epochs. This is only used if the underlying</span>
<span class="sd">            estimator supports ``partial_fit``.</span>

<span class="sd">        n_jobs:</span>
<span class="sd">            Number of parallel jobs. :obj:`-1` means using all processors.</span>

<span class="sd">        n_trials:</span>
<span class="sd">            Number of trials. If :obj:`None`, there is no limitation on the</span>
<span class="sd">            number of trials. If :obj:`timeout` is also set to :obj:`None`,</span>
<span class="sd">            the study continues to create trials until it receives a</span>
<span class="sd">            termination signal such as Ctrl+C or SIGTERM. This trades off</span>
<span class="sd">            runtime vs quality of the solution.</span>

<span class="sd">        random_state:</span>
<span class="sd">            Seed of the pseudo random number generator. If int, this is the</span>
<span class="sd">            seed used by the random number generator. If</span>
<span class="sd">            ``numpy.random.RandomState`` object, this is the random number</span>
<span class="sd">            generator. If :obj:`None`, the global random state from</span>
<span class="sd">            ``numpy.random`` is used.</span>

<span class="sd">        refit:</span>
<span class="sd">            If :obj:`True`, refit the estimator with the best found</span>
<span class="sd">            hyperparameters. The refitted estimator is made available at the</span>
<span class="sd">            ``best_estimator_`` attribute and permits using ``predict``</span>
<span class="sd">            directly.</span>

<span class="sd">        return_train_score:</span>
<span class="sd">            If :obj:`True`, training scores will be included. Computing</span>
<span class="sd">            training scores is used to get insights on how different</span>
<span class="sd">            hyperparameter settings impact the overfitting/underfitting</span>
<span class="sd">            trade-off. However computing training scores can be</span>
<span class="sd">            computationally expensive and is not strictly required to select</span>
<span class="sd">            the hyperparameters that yield the best generalization</span>
<span class="sd">            performance.</span>

<span class="sd">        scoring:</span>
<span class="sd">            String or callable to evaluate the predictions on the validation data.</span>
<span class="sd">            If :obj:`None`, ``score`` on the estimator is used.</span>

<span class="sd">        study:</span>
<span class="sd">            Study corresponds to the optimization task. If :obj:`None`, a new</span>
<span class="sd">            study is created.</span>

<span class="sd">        subsample:</span>
<span class="sd">            Proportion of samples that are used during hyperparameter search.</span>

<span class="sd">            - If int, then draw ``subsample`` samples.</span>
<span class="sd">            - If float, then draw ``subsample`` * ``X.shape[0]`` samples.</span>

<span class="sd">        timeout:</span>
<span class="sd">            Time limit in seconds for the search of appropriate models. If</span>
<span class="sd">            :obj:`None`, the study is executed without time limitation. If</span>
<span class="sd">            :obj:`n_trials` is also set to :obj:`None`, the study continues to</span>
<span class="sd">            create trials until it receives a termination signal such as</span>
<span class="sd">            Ctrl+C or SIGTERM. This trades off runtime vs quality of the</span>
<span class="sd">            solution.</span>

<span class="sd">        verbose:</span>
<span class="sd">            Verbosity level. The higher, the more messages.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        best_estimator_:</span>
<span class="sd">            Estimator that was chosen by the search. This is present only if</span>
<span class="sd">            ``refit`` is set to :obj:`True`.</span>

<span class="sd">        n_splits_:</span>
<span class="sd">            Number of cross-validation splits.</span>

<span class="sd">        refit_time_:</span>
<span class="sd">            Time for refitting the best estimator. This is present only if</span>
<span class="sd">            ``refit`` is set to :obj:`True`.</span>

<span class="sd">        sample_indices_:</span>
<span class="sd">            Indices of samples that are used during hyperparameter search.</span>

<span class="sd">        scorer_:</span>
<span class="sd">            Scorer function.</span>

<span class="sd">        study_:</span>
<span class="sd">            Actual study.</span>

<span class="sd">    Examples:</span>

<span class="sd">        .. testcode::</span>

<span class="sd">                import optuna</span>
<span class="sd">                from sklearn.datasets import load_iris</span>
<span class="sd">                from sklearn.svm import SVC</span>

<span class="sd">                clf = SVC(gamma=&#39;auto&#39;)</span>
<span class="sd">                param_distributions = {</span>
<span class="sd">                    &#39;C&#39;: optuna.distributions.LogUniformDistribution(1e-10, 1e+10)</span>
<span class="sd">                }</span>
<span class="sd">                optuna_search = optuna.integration.OptunaSearchCV(</span>
<span class="sd">                    clf,</span>
<span class="sd">                    param_distributions</span>
<span class="sd">                )</span>
<span class="sd">                X, y = load_iris(return_X_y=True)</span>
<span class="sd">                optuna_search.fit(X, y)</span>
<span class="sd">                y_pred = optuna_search.predict(X)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_required_parameters</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;estimator&quot;</span><span class="p">,</span> <span class="s2">&quot;param_distributions&quot;</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">_estimator_type</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; str</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="o">.</span><span class="n">_estimator_type</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_index_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; int</span>
        <span class="sd">&quot;&quot;&quot;Index which corresponds to the best candidate parameter setting.&quot;&quot;&quot;</span>

        <span class="n">df</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">trials_dataframe</span><span class="p">()</span>

        <span class="k">return</span> <span class="n">df</span><span class="p">[</span><span class="s2">&quot;value&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_params_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;Parameters of the best trial in the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">best_params</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_score_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Mean cross-validated score of the best estimator.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">best_value</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">best_trial_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; FrozenTrial</span>
        <span class="sd">&quot;&quot;&quot;Best trial in the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">best_trial</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">classes_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; OneDimArrayLikeType</span>
        <span class="sd">&quot;&quot;&quot;Class labels.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">classes_</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">n_trials_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; int</span>
        <span class="sd">&quot;&quot;&quot;Actual number of trials.&quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">trials_</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">trials_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; List[FrozenTrial]</span>
        <span class="sd">&quot;&quot;&quot;All trials in the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">trials</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">user_attrs_</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, Any]</span>
        <span class="sd">&quot;&quot;&quot;User attributes in the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">user_attrs</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">decision_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
        <span class="sd">&quot;&quot;&quot;Call ``decision_function`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``decision_function`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">decision_function</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">inverse_transform</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., TwoDimArrayLikeType]</span>
        <span class="sd">&quot;&quot;&quot;Call ``inverse_transform`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``inverse_transform`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">inverse_transform</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
        <span class="sd">&quot;&quot;&quot;Call ``predict`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports ``predict``</span>
<span class="sd">        and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">predict</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">predict_log_proba</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., TwoDimArrayLikeType]</span>
        <span class="sd">&quot;&quot;&quot;Call ``predict_log_proba`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``predict_log_proba`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">predict_log_proba</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">predict_proba</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., TwoDimArrayLikeType]</span>
        <span class="sd">&quot;&quot;&quot;Call ``predict_proba`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``predict_proba`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">predict_proba</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">score_samples</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., OneDimArrayLikeType]</span>
        <span class="sd">&quot;&quot;&quot;Call ``score_samples`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``score_samples`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">score_samples</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">set_user_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., None]</span>
        <span class="sd">&quot;&quot;&quot;Call ``set_user_attr`` on the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">set_user_attr</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">transform</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., TwoDimArrayLikeType]</span>
        <span class="sd">&quot;&quot;&quot;Call ``transform`` on the best estimator.</span>

<span class="sd">        This is available only if the underlying estimator supports</span>
<span class="sd">        ``transform`` and ``refit`` is set to :obj:`True`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">transform</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">trials_dataframe</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Callable[..., pd.DataFrame]</span>
        <span class="sd">&quot;&quot;&quot;Call ``trials_dataframe`` on the :class:`~optuna.study.Study`.&quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_is_fitted</span><span class="p">()</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">trials_dataframe</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">estimator</span><span class="p">,</span>  <span class="c1"># type: BaseEstimator</span>
        <span class="n">param_distributions</span><span class="p">,</span>  <span class="c1"># type: Mapping[str, distributions.BaseDistribution]</span>
        <span class="n">cv</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[BaseCrossValidator, int]]</span>
        <span class="n">enable_pruning</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">error_score</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span>  <span class="c1"># type: Union[Number, str]</span>
        <span class="n">max_iter</span><span class="o">=</span><span class="mi">1000</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">n_jobs</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">n_trials</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span>  <span class="c1"># type: int</span>
        <span class="n">random_state</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[int, np.random.RandomState]]</span>
        <span class="n">refit</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">return_train_score</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>  <span class="c1"># type: bool</span>
        <span class="n">scoring</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[Callable[..., float], str]]</span>
        <span class="n">study</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[study_module.Study]</span>
        <span class="n">subsample</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>  <span class="c1"># type: Union[float, int]</span>
        <span class="n">timeout</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[float]</span>
        <span class="n">verbose</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>  <span class="c1"># type: int</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; None</span>

        <span class="n">_imports</span><span class="o">.</span><span class="n">check</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">cv</span> <span class="o">=</span> <span class="n">cv</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">enable_pruning</span> <span class="o">=</span> <span class="n">enable_pruning</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span> <span class="o">=</span> <span class="n">error_score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">estimator</span> <span class="o">=</span> <span class="n">estimator</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span> <span class="o">=</span> <span class="n">max_iter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n_trials</span> <span class="o">=</span> <span class="n">n_trials</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">n_jobs</span> <span class="o">=</span> <span class="n">n_jobs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span> <span class="o">=</span> <span class="n">param_distributions</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">random_state</span> <span class="o">=</span> <span class="n">random_state</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">refit</span> <span class="o">=</span> <span class="n">refit</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span> <span class="o">=</span> <span class="n">return_train_score</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scoring</span> <span class="o">=</span> <span class="n">scoring</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="o">=</span> <span class="n">study</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subsample</span> <span class="o">=</span> <span class="n">subsample</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">timeout</span> <span class="o">=</span> <span class="n">timeout</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="n">verbose</span>

    <span class="k">def</span> <span class="nf">_check_is_fitted</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="n">attributes</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;n_splits_&quot;</span><span class="p">,</span> <span class="s2">&quot;sample_indices_&quot;</span><span class="p">,</span> <span class="s2">&quot;scorer_&quot;</span><span class="p">,</span> <span class="s2">&quot;study_&quot;</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">refit</span><span class="p">:</span>
            <span class="n">attributes</span> <span class="o">+=</span> <span class="p">[</span><span class="s2">&quot;best_estimator_&quot;</span><span class="p">,</span> <span class="s2">&quot;refit_time_&quot;</span><span class="p">]</span>

        <span class="n">check_is_fitted</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">attributes</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_check_params</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; None</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">,</span> <span class="s2">&quot;fit&quot;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;estimator must be a scikit-learn estimator.&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">dict</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;param_distributions must be a dictionary.&quot;</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">distribution</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">distribution</span><span class="p">,</span> <span class="n">distributions</span><span class="o">.</span><span class="n">BaseDistribution</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Value of </span><span class="si">{}</span><span class="s2"> must be a optuna distribution.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">enable_pruning</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">,</span> <span class="s2">&quot;partial_fit&quot;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;estimator must support partial_fit.&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;max_iter must be &gt; 0, got </span><span class="si">{}</span><span class="s2">.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span><span class="o">.</span><span class="n">direction</span> <span class="o">!=</span> <span class="n">StudyDirection</span><span class="o">.</span><span class="n">MAXIMIZE</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;direction of study must be &#39;maximize&#39;.&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_more_tags</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># type: () -&gt; Dict[str, bool]</span>

        <span class="k">return</span> <span class="p">{</span><span class="s2">&quot;non_deterministic&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> <span class="s2">&quot;no_validation&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}</span>

    <span class="k">def</span> <span class="nf">_refit</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: TwoDimArrayLikeType</span>
        <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
        <span class="o">**</span><span class="n">fit_params</span>  <span class="c1"># type: Any</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; &#39;OptunaSearchCV&#39;</span>

        <span class="n">n_samples</span> <span class="o">=</span> <span class="n">_num_samples</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span> <span class="o">=</span> <span class="n">clone</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">set_params</span><span class="p">(</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">best_params</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">exception</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Refitting the estimator using </span><span class="si">{}</span><span class="s2"> samples...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">n_samples</span><span class="p">))</span>

        <span class="n">start_time</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">fit_params</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">refit_time_</span> <span class="o">=</span> <span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">start_time</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Finished refitting! (elapsed time: </span><span class="si">{:.3f}</span><span class="s2"> sec.)&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">refit_time_</span><span class="p">))</span>

        <span class="k">return</span> <span class="bp">self</span>

<div class="viewcode-block" id="OptunaSearchCV.fit"><a class="viewcode-back" href="../../../reference/integration.html#optuna.integration.OptunaSearchCV.fit">[文档]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: TwoDimArrayLikeType</span>
        <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
        <span class="n">groups</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[OneDimArrayLikeType]</span>
        <span class="o">**</span><span class="n">fit_params</span>  <span class="c1"># type: Any</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; &#39;OptunaSearchCV&#39;</span>
        <span class="sd">&quot;&quot;&quot;Run fit with all sets of parameters.</span>

<span class="sd">        Args:</span>
<span class="sd">            X:</span>
<span class="sd">                Training data.</span>

<span class="sd">            y:</span>
<span class="sd">                Target variable.</span>

<span class="sd">            groups:</span>
<span class="sd">                Group labels for the samples used while splitting the dataset</span>
<span class="sd">                into train/validation set.</span>

<span class="sd">            **fit_params:</span>
<span class="sd">                Parameters passed to ``fit`` on the estimator.</span>

<span class="sd">        Returns:</span>
<span class="sd">            self:</span>
<span class="sd">                Return self.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_check_params</span><span class="p">()</span>

        <span class="n">random_state</span> <span class="o">=</span> <span class="n">check_random_state</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">random_state</span><span class="p">)</span>
        <span class="n">max_samples</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">subsample</span>
        <span class="n">n_samples</span> <span class="o">=</span> <span class="n">_num_samples</span><span class="p">(</span><span class="n">X</span><span class="p">)</span>
        <span class="n">old_level</span> <span class="o">=</span> <span class="n">_logger</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">DEBUG</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">INFO</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">WARNING</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">n_samples</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">max_samples</span><span class="p">)</span> <span class="ow">is</span> <span class="nb">float</span><span class="p">:</span>
            <span class="n">max_samples</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">max_samples</span> <span class="o">*</span> <span class="n">n_samples</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">max_samples</span> <span class="o">&lt;</span> <span class="n">n_samples</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span> <span class="o">=</span> <span class="n">random_state</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">,</span> <span class="n">max_samples</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="kc">False</span>
            <span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

        <span class="n">X_res</span> <span class="o">=</span> <span class="n">_safe_indexing</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">)</span>
        <span class="n">y_res</span> <span class="o">=</span> <span class="n">_safe_indexing</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">)</span>
        <span class="n">groups_res</span> <span class="o">=</span> <span class="n">_safe_indexing</span><span class="p">(</span><span class="n">groups</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">)</span>
        <span class="n">fit_params_res</span> <span class="o">=</span> <span class="n">fit_params</span>

        <span class="k">if</span> <span class="n">fit_params_res</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">fit_params_res</span> <span class="o">=</span> <span class="n">_check_fit_params</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">fit_params</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">)</span>

        <span class="n">classifier</span> <span class="o">=</span> <span class="n">is_classifier</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">)</span>
        <span class="n">cv</span> <span class="o">=</span> <span class="n">check_cv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cv</span><span class="p">,</span> <span class="n">y_res</span><span class="p">,</span> <span class="n">classifier</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">n_splits_</span> <span class="o">=</span> <span class="n">cv</span><span class="o">.</span><span class="n">get_n_splits</span><span class="p">(</span><span class="n">X_res</span><span class="p">,</span> <span class="n">y_res</span><span class="p">,</span> <span class="n">groups</span><span class="o">=</span><span class="n">groups_res</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">scorer_</span> <span class="o">=</span> <span class="n">check_scoring</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">,</span> <span class="n">scoring</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">scoring</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">seed</span> <span class="o">=</span> <span class="n">random_state</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">iinfo</span><span class="p">(</span><span class="s2">&quot;int32&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
            <span class="n">sampler</span> <span class="o">=</span> <span class="n">samplers</span><span class="o">.</span><span class="n">TPESampler</span><span class="p">(</span><span class="n">seed</span><span class="o">=</span><span class="n">seed</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">study_</span> <span class="o">=</span> <span class="n">study_module</span><span class="o">.</span><span class="n">create_study</span><span class="p">(</span><span class="n">direction</span><span class="o">=</span><span class="s2">&quot;maximize&quot;</span><span class="p">,</span> <span class="n">sampler</span><span class="o">=</span><span class="n">sampler</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">study_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">study</span>

        <span class="n">objective</span> <span class="o">=</span> <span class="n">_Objective</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">estimator</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">param_distributions</span><span class="p">,</span>
            <span class="n">X_res</span><span class="p">,</span>
            <span class="n">y_res</span><span class="p">,</span>
            <span class="n">cv</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">enable_pruning</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error_score</span><span class="p">,</span>
            <span class="n">fit_params_res</span><span class="p">,</span>
            <span class="n">groups_res</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">max_iter</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">return_train_score</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">scorer_</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
            <span class="s2">&quot;Searching the best hyperparameters using </span><span class="si">{}</span><span class="s2"> &quot;</span>
            <span class="s2">&quot;samples...&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">_num_samples</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">sample_indices_</span><span class="p">))</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">study_</span><span class="o">.</span><span class="n">optimize</span><span class="p">(</span>
            <span class="n">objective</span><span class="p">,</span> <span class="n">n_jobs</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_jobs</span><span class="p">,</span> <span class="n">n_trials</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">n_trials</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">timeout</span>
        <span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Finished hyperparemeter search!&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">refit</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_refit</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="o">**</span><span class="n">fit_params</span><span class="p">)</span>

        <span class="n">_logger</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">old_level</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span></div>

<div class="viewcode-block" id="OptunaSearchCV.score"><a class="viewcode-back" href="../../../reference/integration.html#optuna.integration.OptunaSearchCV.score">[文档]</a>    <span class="k">def</span> <span class="nf">score</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">X</span><span class="p">,</span>  <span class="c1"># type: TwoDimArrayLikeType</span>
        <span class="n">y</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>  <span class="c1"># type: Optional[Union[OneDimArrayLikeType, TwoDimArrayLikeType]]</span>
    <span class="p">):</span>
        <span class="c1"># type: (...) -&gt; float</span>
        <span class="sd">&quot;&quot;&quot;Return the score on the given data.</span>

<span class="sd">        Args:</span>
<span class="sd">            X:</span>
<span class="sd">                Data.</span>

<span class="sd">            y:</span>
<span class="sd">                Target variable.</span>

<span class="sd">        Returns:</span>
<span class="sd">            score:</span>
<span class="sd">                Scaler score.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">scorer_</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">best_estimator_</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, Optuna Contributors.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
    <a href="../../../privacy.html">Privacy Policy</a>.
     


</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-55135190-8', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>